package com.docjohnson.pamphlet.utils;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.os.Environment;
import android.util.Log;

public class NetUtils {
	private static final String TAG = NetUtils.class.getSimpleName();

	public static String getResponse(String url) {
		File responseCacheFile = new File(Environment.getExternalStorageDirectory() + "/pamphlets/responses/pamphletsResponse.txt");

//		if (responseCacheFile.exists()) {
//			try {
//				String result = readFileAsString(responseCacheFile);
//				return result;
//			} catch (IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//		}
		HttpGet get = new HttpGet(url);

		HttpClient client = new DefaultHttpClient();

		HttpResponse response = null;
		try {
			response = client.execute(get);
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		HttpEntity entity = response.getEntity();

		if (entity != null) {
			try {
				String responseString = EntityUtils.toString(entity);
				saveStringToSdCard(responseString, Environment.getExternalStorageDirectory() + "/pamphlets/responses", "pamphletsResponse.txt");
				return responseString;
			} catch (ParseException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return null;
	}

	public static void downloadFile(String fileUrl, String filePath) {
		Log.d(TAG, "downloading file " + fileUrl + " to " + filePath);
		try {
			File fileParent = new File(filePath).getParentFile();
			
			if(!fileParent.exists()){
				fileParent.mkdirs();
			}
			
			URL url = new URL(fileUrl);
			URLConnection connection = url.openConnection();
			connection.connect();

			InputStream input = new BufferedInputStream(url.openStream());
			OutputStream output = new FileOutputStream(filePath);

			byte data[] = new byte[1024];
			int count;
			while ((count = input.read(data)) != -1) {
				output.write(data, 0, count);
			}

			output.flush();
			output.close();
			input.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static void saveStringToSdCard(String stringToSave, String folder, String fileName) throws IOException {
		File textFileFolder = new File(folder);
		textFileFolder.mkdir();
		textFileFolder.mkdirs();
		File textFile = new File(folder, fileName);
		textFile.createNewFile();
		PrintWriter out = new PrintWriter(textFile);
		out.print(stringToSave);
		out.close();
	}

	public static String readFileAsString(File file) throws java.io.IOException {
		BufferedReader reader = new BufferedReader(new FileReader(file));
		String line, results = "";
		while ((line = reader.readLine()) != null) {
			results += line;
		}
		reader.close();
		return results;
	}
}